# 定义变量
IMAGE_NAME := ginic
CONTAINER_NAME := ginic
TARGET_NAME := app
PORT = 8080
# 默认目标
.PHONY: help
help:
	@echo "usage: make <option>"
	@echo "options and effects:"
	@echo "    help   	: Show help"
	@echo "    linux  	: Build the linux binary of this project"
	@echo "    windows	: Build the windows binary of this project"
	@echo "    build 	: Build the darwin binary of this project"

# 构建linux
.PHONY: linux
linux:
	@CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -a -installsuffix cgo -o ${TARGET_NAME} .

# 构建windows
.PHONY: windows
windows:
	@CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="-w -s" -a -installsuffix cgo -o ${TARGET_NAME} .

# 运行单元测试
.PHONY: test
test:
	@go test .

# 构建 Docker 镜像
.PHONY: build
TAG = latest
build:
	@docker build -t $(IMAGE_NAME):$(TAG) .
	docker images |grep $(IMAGE_NAME)

# 启动容器
.PHONY: run
TAG = latest
run: rm
	@docker run -d --name $(CONTAINER_NAME) -p $(PORT):8080 $(IMAGE_NAME):$(TAG)

# 终止容器
.PHONY: stop
CONTAINER_ID = $(shell docker ps -q -a -f name=$(CONTAINER_NAME))
stop:
	@if [ -n "$(CONTAINER_ID)" ]; then \
		echo "存在正在运行的$(CONTAINER_NAME)容器:$(CONTAINER_ID),停止容器："; \
	  	docker stop $(CONTAINER_ID) ; \
	fi

# 删除容器
.PHONY: rm
CONTAINER_ID := $(shell docker ps -q -a -f name=$(CONTAINER_NAME))
rm: stop
	@if [ -n "$(CONTAINER_ID)" ]; then \
		docker rm $(CONTAINER_ID) ; \
	fi